﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DroidClaim.BL.Utility
{
    public class GeometricDistance
    {
        public const double EarthRadiusInMiles = 3956.0;
        public const double EarthRadiusInKilometers = 6367.0;

        public static double CalcDistanceInKilometer(double lat1, double lng1, double lat2, double lng2) 
        {
            double radius = EarthRadiusInKilometers;

            return radius * 2 * Math.Asin( Math.Min(1, Math.Sqrt( ( Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
        }

        public static double CalcDistanceInMile(double lat1, double lng1, double lat2, double lng2)
        {
            double radius = EarthRadiusInMiles;
            return radius * 2 * Math.Asin(Math.Min(1, Math.Sqrt((Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0)))));
        }

        private static double ToRadian(double val) { return val * (Math.PI / 180); }
        private static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }
    }
}